#include <bits/stdc++.h>
using namespace std;
#define int long long
int n,k;
vector<int>v,w;
// set<int>s;
unordered_map<int, bool>mp;
bool check(int x)
{
    if(x>=2*k)x-=2*k;
	int y=x+2*k;
	for(auto z:mp)
	{
		if((x>=z.first&&x<=z.first+k-1)||(y>=z.first&&y<=z.first+k-1)){}
		else return 0;
	}
	return 1;
}
void solve()
{
    mp.clear();
	cin>>n>>k;
	v=w=vector<int>(n+1);
	int mx=0;
	//vector<bool>f(2*k+1);
	for(int i=1;i<=n;i++)
	{
		cin>>v[i];
		w[i]=v[i]%(2*k);
        mp[w[i]]=1;
		mx=max(mx,v[i]);
	}
	int l=mx%(2*k),r=(mx+k)%(2*k),mid;
	if(r<=l)r+=2*k;
	int tmp=r;
	while(!check(l)&&l<r)
	{
		l++;
	}
	if(l!=tmp)cout<<mx-(tmp-k)+l<<endl;
	else cout<<-1<<endl;
}
signed main()
{
	//ios::sync_with_stdio(false);
	//cin.tie(nullptr);
	int T=1;
	cin>>T;
	while(T--)
	{
		solve();
	}
	return 0;
}